package com.blithe.wgt.ui.activity;

import android.os.Bundle;

import com.blithe.wgt.R;
import com.blithe.wgt.core.BaseActivity;
import com.blithe.wgt.entity.Path;
import com.blithe.wgt.http.ProgressSubscriber;
import com.blithe.wgt.http.SubscriberOnNextListener;
import com.blithe.wgt.http.response.BaseResponse;
import com.blithe.wgt.httpService.TaskLoader;
import com.blithe.wgt.listener.OnItemClickListener;
import com.blithe.wgt.ui.adapter.PathAdapter;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ChosePathActivity extends BaseActivity {

    private PathAdapter mPathAdapter;
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_chose_path;
    }

    @Override
    protected void initView() {

        setTitle("选择路径");
        refreshLayout=findViewById(R.id.refresh_layout);
        recyclerView=findViewById(R.id.rv_path_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(true);
        hideBackBtn();
    }

    @Override
    protected void initData() {
        mPathAdapter= new PathAdapter(new ArrayList<>(),mContext);
        recyclerView.setAdapter(mPathAdapter);
        new TaskLoader().getPathList().subscribe(new ProgressSubscriber<List<Path>>(subscriberOnNextListener,mContext));
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                new TaskLoader().getPathList().subscribe(new ProgressSubscriber<List<Path>>(subscriberOnNextListener,mContext));
            }
        });
        mPathAdapter.setOnItemClickListener(onItemClickListener);
    }
    private OnItemClickListener<Path> onItemClickListener=new OnItemClickListener<Path>() {
        @Override
        public void OnClick(Path pathEntity) {
            EventBus.getDefault().post(pathEntity);
            ChosePathActivity.this.finish();
        }
    };
    private SubscriberOnNextListener<List<Path>> subscriberOnNextListener=new SubscriberOnNextListener<List<Path>>(){

        @Override
        public void onNext(BaseResponse<List<Path>> response) {

                Logger.e(new Gson().toJson(response));
                mPathAdapter.setPathEntities(response.data);
                mPathAdapter.notifyDataSetChanged();

        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onError(String msg) {

        }
    };
}
